package cn.pli.bike;

import android.content.Intent;
import android.os.StrictMode;

import com.framemodule.BaseApp.BaseApp;
import com.framemodule.BaseApp.Setting;
import com.framemodule.base.ImageLoaderHelper;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.uuzuche.lib_zxing.activity.ZXingLibrary;

import java.io.File;

import cn.jpush.android.api.JPushInterface;
import cn.pli.bike.service.ConnectService;


/**
 * Created by pc on 2017/9/19.
 */

public class App extends BaseApp {

    private static Setting setting = null;
    private static App app;

    public static App getApp() {
        return app;
    }

    public static Setting getShareSetting() {
        return setting;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        initImageLoader();

        initService();
        ZXingLibrary.initDisplayOpinion(this);

        this.app = this;
        setting = new Setting(this, "KHBIKE");

        JPushInterface.setDebugMode(true);    // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this);            // 初始化 JPush

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        builder.detectFileUriExposure();

        //    LeakCanary.install(this);
    }


    private void initImageLoader() {
        File cacheDir = StorageUtils.getOwnCacheDirectory(
                getApplicationContext(), "bitmap/Cache");
        //  ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                this)
                .threadPoolSize(7)//3
                // default
                .threadPriority(Thread.NORM_PRIORITY - 2)
                // default
                .tasksProcessingOrder(QueueProcessingType.FIFO)
                // default
                .denyCacheImageMultipleSizesInMemory()
                .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                .memoryCacheSize(2 * 1024 * 1024).memoryCacheSizePercentage(13)
                // default
                .diskCache(new UnlimitedDiskCache(cacheDir))
                // default
                .diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(100)
                .diskCacheFileNameGenerator(new Md5FileNameGenerator()) // default
                .defaultDisplayImageOptions(ImageLoaderHelper.getInstance(this).getDisplayOptionsLittle()) // default
                .build();
        ImageLoader.getInstance().init(config);
    }

    private void initService() {
        Intent intent = new Intent(this, ConnectService.class);
        startService(intent);
    }


}
